package com.onetime.mall.trading.controller;

import com.onetime.mall.trading.constants.ErrorMsg;
import com.onetime.mall.trading.entity.AdminEntity;
import com.onetime.mall.trading.entity.IdleItemEntity;
import com.onetime.mall.trading.entity.UserEntity;
import com.onetime.mall.trading.service.AdminService;
import com.onetime.mall.trading.service.IdleItemService;
import com.onetime.mall.trading.service.OrderService;
import com.onetime.mall.trading.service.UserService;
import com.onetime.mall.trading.utils.PageUtils;
import com.onetime.mall.trading.utils.R;
import com.onetime.mall.trading.vo.StatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 管理员表
 *
 * @author liyixuan
 * @email liyixuan106@163.com
 * @date 2022-03-26 19:38:29
 */
@RestController
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private IdleItemService idleItemService;
    @Autowired
    private UserService userService;

    @PostMapping("add")
    public R addAdmin(@RequestBody AdminEntity adminEntity, HttpSession session) {
        if (session.getAttribute("admin") == null) {
            return R.error(ErrorMsg.COOKIE_ERROR.getCode(), ErrorMsg.COOKIE_ERROR.getMsg());
        }
        if (adminService.addAdmin(adminEntity)) {
            return R.ok();
        }
        return R.error(ErrorMsg.PARAM_ERROR.getCode(), ErrorMsg.PARAM_ERROR.getMsg());
    }

    @PostMapping("adding")
    public R adding(@RequestBody AdminEntity adminEntity, HttpSession session) {
        if (session.getAttribute("admin") == null) {
            return R.error(ErrorMsg.COOKIE_ERROR.getCode(), ErrorMsg.COOKIE_ERROR.getMsg());
        }
        if (adminService.addAdmin(adminEntity)) {
            return R.ok();
        }
        return R.error(ErrorMsg.PARAM_ERROR.getCode(), ErrorMsg.PARAM_ERROR.getMsg());
    }

    /**
     * // TODO: 2022/3/29 分页查看
     * <p>
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        System.out.println("管理员列表" + params);
        PageUtils page = adminService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * // TODO: 2022/3/29 分页
     *
     * @param session
     * @param params
     * @return
     */
    @GetMapping("idleList")
    public R idleList(HttpSession session, @RequestParam Map<String, Object> params) {
        if (session.getAttribute("admin") == null) {
            return R.error(ErrorMsg.COOKIE_ERROR.getCode(), ErrorMsg.COOKIE_ERROR.getMsg());
        }
        System.out.println("所有商品列表" + params);
        PageUtils page = idleItemService.adminGetIdleListByPage(params);
        return R.ok().put("page", page);
    }

    /**
     * // TODO: 2022/3/30 分页
     *
     * @param session
     * @param params  分页参数
     * @return
     */
    @GetMapping("orderList")
    public R orderList(HttpSession session, @RequestParam Map<String, Object> params) {
        if (session.getAttribute("admin") == null) {
            return R.error(ErrorMsg.COOKIE_ERROR.getCode(), ErrorMsg.COOKIE_ERROR.getMsg());
        }
        System.out.println("管理员订单列表：" + params);
        PageUtils page = orderService.getAllOrderByPage(params);
        return R.ok().put("page", page);
    }

    /**
     * // TODO: 2022/3/30 分页
     *
     * @param session
     * @param params
     * @return
     */
    @GetMapping("userList")
    public R userList(HttpSession session, @RequestParam Map<String, Object> params) {
        if (session.getAttribute("admin") == null) {
            return R.error(ErrorMsg.COOKIE_ERROR.getCode(), ErrorMsg.COOKIE_ERROR.getMsg());
        }
        System.out.println("用户数量：" + params);
        PageUtils page = userService.getUserByStatusByPage(params);
        return R.ok().put("page", page);
    }

    @GetMapping("login")
    public R login(@RequestParam("accountNumber") @NotNull @NotEmpty String accountNumber,
                   @RequestParam("adminPassword") @NotNull @NotEmpty String adminPassword,
                   HttpSession session) {
        AdminEntity adminEntity = adminService.login(accountNumber, adminPassword);
        if (null == adminEntity) {
            return R.error(ErrorMsg.EMAIL_LOGIN_ERROR.getCode(), ErrorMsg.EMAIL_LOGIN_ERROR.getMsg());
        }
        session.setAttribute("admin", adminEntity);
        return R.ok().put("data", adminEntity);
    }

    @GetMapping("loginOut")
    public R loginOut(HttpSession session) {
        /*清空指定的属性 ,但是session还存在*/
        session.removeAttribute("admin");
        /*清除当前session的所有相关信息*/
        session.invalidate();
        return R.ok();
    }


    /**
     * 更新商品状态
     *
     * @param session
     * @param id
     * @param status
     * @return
     */
    @GetMapping("updateIdleStatus")
    public R updateIdleStatus(HttpSession session,
                              @RequestParam("id") @NotNull @NotEmpty Long id,
                              @RequestParam("status") @NotNull @NotEmpty Integer status) {
        /*管理员未登录*/
        /*if (session.getAttribute("admin") == null) {
            return R.error(ErrorMsg.COOKIE_ERROR.getCode(), ErrorMsg.COOKIE_ERROR.getMsg());
        }*/
        IdleItemEntity idleItemEntity = new IdleItemEntity();
        idleItemEntity.setId(id);
        idleItemEntity.setIdleStatus(status);
        if (idleItemService.updateIdleItem(idleItemEntity)) {
            return R.ok();
        }
        return R.error(ErrorMsg.SYSTEM_ERROR.getCode(), ErrorMsg.SYSTEM_ERROR.getMsg());
    }

    /**
     * 更新用户状态
     *
     * @param session
     * @param id
     * @param status
     * @return
     */
    @GetMapping("updateUserStatus")
    public R updateUserStatus(HttpSession session,
                              @RequestParam("id") @NotNull @NotEmpty Long id,
                              @RequestParam("status") @NotNull @NotEmpty boolean status) {
        /*管理员未登录*/
        if (session.getAttribute("admin") == null) {
            return R.error(ErrorMsg.COOKIE_ERROR.getCode(), ErrorMsg.COOKIE_ERROR.getMsg());
        }
        UserEntity user = new UserEntity();
        user.setId(id);
        user.setUserStatus(status);
        if (userService.updateUserInfo(user)) {
            return R.ok();
        }
        return R.error(ErrorMsg.SYSTEM_ERROR.getCode(), ErrorMsg.SYSTEM_ERROR.getMsg());
    }

    /**
     * 数据统计
     *
     * @param session session
     * @return 统计数据
     */
    @RequestMapping("statistics")
    public R statistics(HttpSession session, @RequestParam("status") Integer status) {
        if (session.getAttribute("admin") == null) {
            return R.error(ErrorMsg.COOKIE_ERROR.getCode(), ErrorMsg.COOKIE_ERROR.getMsg());
        }
        List<StatisticsVo> data = idleItemService.getProductCategoryStatistics(status);
        return R.ok().put("data", data);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        AdminEntity admin = adminService.getById(id);

        return R.ok().put("admin", admin);
    }

    @RequestMapping("/get")
    public R get(@RequestParam("id") Long id) {
        AdminEntity admin = adminService.getById(id);

        return R.ok().put("admin", admin);
    }

    /**
     * 保存
     */
    @PostMapping ("/save")
    public R save(@RequestParam Map<String, Object> params) {
        System.out.println(params.toString());
        String id = params.get("id").toString();
        Long value = Long.valueOf(id);
        String accountNumber = params.get("accountNumber").toString();
        String adminPassword = params.get("adminPassword").toString();
        String adminName = params.get("adminName").toString();
        AdminEntity admin = new AdminEntity(value, accountNumber, adminPassword, adminName);
        if (value == 0) {
            adminService.save(admin);
        } else {
            adminService.updateById(admin);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AdminEntity admin) {
        adminService.updateById(admin);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam("data") String ids) {
        String[] split = ids.split(",");
        List<Long> collect = Arrays.stream(split).map(Long::valueOf).collect(Collectors.toList());
        adminService.removeByIds(collect);

        return R.ok();
    }

    @GetMapping("deleteOrder")
    public R deleteOrder(@RequestParam("id") @NotNull @NotEmpty Long id, HttpSession session) {
        if (session.getAttribute("admin") == null) {
            return R.error(ErrorMsg.COOKIE_ERROR.getCode(), ErrorMsg.COOKIE_ERROR.getMsg());
        }
        if (orderService.deleteOrder(id)) {
            return R.ok();
        }
        return R.error(ErrorMsg.SYSTEM_ERROR.getCode(), ErrorMsg.SYSTEM_ERROR.getMsg());
    }

}
